package com.shopping.cart.auth

import com.netflix.api.client.NetflixAPIClient
import com.netflix.api.NetflixAPIResponse
import com.netflix.api.NetflixAPIException
import com.netflix.api.client.APIConstants


class PostController {

    String myConsumerKey = '9kuckqctuh35jumnsu2kpdhk'
    String myConsumerSecret = '7b3fZ9Hb6F'

    def index = {
        render "Hello world - this should have been locked down"
    }

    def movieInfo = {
        NetflixAPIClient apiClient = new NetflixAPIClient(myConsumerKey, myConsumerSecret);
        String uri = APIConstants.CATALOG_URI
//        String uri = "http://api.netflix.com/catalog/title"
        String movieName = 'Waterworld'
        Map callParameters = ['term': movieName]
////        callParameters.put("term", "Nathan Fillion")

//String uri = "http://api.netflix.com/catalog/people";
//  HashMap<String, String> callParameters = new HashMap<String, String>();
//  callParameters.put("term", "Nathan Fillion");


        String results = null
        try {
            NetflixAPIResponse netFlixResponse = apiClient.makeConsumerSignedApiCall(uri, callParameters, NetflixAPIClient.GET_METHOD_TYPE)
            results = netFlixResponse.getResponse()
            List titles = parseXml(results)
            int statusCode = netFlixResponse.getStatusCode()
            if (statusCode == 200) {
                // process normally
//                println xmlSlurper
                render view:'movieInfo', model:[results: titles]
            }
            else {
                // display error to user
            }
        }
        catch (NetflixAPIException nae) {
            // handle the error...
        }
    }


    private parseXml(def xml) {
        def nodes = new XmlParser().parseText(xml)
        def catalog_title = nodes.catalog_title
        List results = []

        boolean isFirst = false
        catalog_title.each {
            Map resultData = [:]
            if(it.title) {
                resultData.title = it.title.@regular
            }

            if(it.average_rating) {
                resultData.averageResult = it.average_rating.text()
            }

            if(it.release_year) {
                resultData.releaseYear = it.release_year.text()
            }

//            if(it.link[0].@title && !isFirst) {
            if(it.link[0].@title && !isFirst) {
//                isFirst = true
                resultData.synopsis = getSynopsis( it.link[0].@href )
            }



            results << resultData
        }
        return results
    }


    private String getSynopsis(link) {
        NetflixAPIClient apiClient = new NetflixAPIClient(myConsumerKey, myConsumerSecret);
        NetflixAPIResponse netFlixResponse = apiClient.makeConsumerSignedApiCall(link, [:], NetflixAPIClient.GET_METHOD_TYPE)
        def results = netFlixResponse.getResponse()
        def nodes = new XmlParser().parseText(results)
        return nodes.value()[0]
    }

    private getSimilars(link){
        NetflixAPIClient apiClient = new NetflixAPIClient(myConsumerKey, myConsumerSecret);
        NetflixAPIResponse netFlixResponse = apiClient.makeConsumerSignedApiCall(link, [:], NetflixAPIClient.GET_METHOD_TYPE)
        def results = netFlixResponse.getResponse()
        def nodes = new XmlParser().parseText(results)
        println nodes
    }
}
